package com.example.anew.journey.mynewjourney.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;

import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeCardiacDetection.bean.HistoricalDataViewBean;

import java.util.ArrayList;
import java.util.List;

import cn.jesse.nativelogger.NLogger;

/**
 * 杭州惟君医疗科技有限公司17
 * Created by 20433 on 2018/3/20
 */

public class MyHorizontalScrollView extends HorizontalScrollView {
    private int movingdistance = 0; //移动的总距离
    private int XoffSet = 100; //x轴偏移量

    public void setMlist(List<HistoricalDataViewBean> tlist) {
        mlist.addAll(tlist);
    }

    private List<HistoricalDataViewBean> mlist = new ArrayList<>();

    public MyHorizontalScrollView(Context context) {
        super(context);
    }

    public MyHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        // TODO Auto-generated method stub
        super.onScrollChanged(l, t, oldl, oldt);
        int maxScrollX = getChildAt(0).getMeasuredWidth() - getMeasuredWidth();
        //滑到最左
        if (getScrollX() == 0) {
            onScrollstopListner.onScrollToLeftEdge();
        } else if (getScrollX() == maxScrollX) {  //滑到最右
            onScrollstopListner.onScrollToRightEdge();
        } else {  //滑到中间
            // onScrollstopListner.onScrollToMiddle();
        }
    }

    public interface OnScrollStopListner {
        /**
         * scroll have stoped
         */
        void onScrollStoped();

        /**
         * scroll have stoped, and is at left edge
         */
        void onScrollToLeftEdge();

        /**
         * scroll have stoped, and is at right edge
         */
        void onScrollToRightEdge();

        /**
         * scroll have stoped, and is at middle
         */
        void onScrollToMiddlePoint(HistoricalDataViewBean bean);
    }

    private OnScrollStopListner onScrollstopListner;

    public void setOnScrollStopListner(OnScrollStopListner listner) {
        onScrollstopListner = listner;
    }

    int rawX = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 获取当前触摸的绝对坐标
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: //按下
                rawX = (int) event.getX();
                break;
            case MotionEvent.ACTION_UP: //离开
                // 离开时的坐标
                int lastX = (int) event.getX();
                int movedistance = lastX - rawX;
                calculateMovingGrid(movedistance);
                break;
        }
        return super.onTouchEvent(event);
    }

    private void calculateMovingGrid(int movedistance) {
        movingdistance = movingdistance - movedistance;
         NLogger.e("index","=======calculateMovingGrid====总==="+movingdistance);
        if (movingdistance < 0) {  //滑动到最左边
            movingdistance = 0;
            getInformationoint(movingdistance);
        } else {  //根据滑动的位移计算与中间线交界的点的信息
            getInformationoint(movingdistance);
        }
    }
    private void getInformationoint(int movingdistance) {
        scrollTo( movingdistance,0);
        postInvalidateDelayed(10);
        if (mlist.size() > 4) {
            if (onScrollstopListner != null) {
                if (movingdistance == 0) {
                    onScrollstopListner.onScrollToMiddlePoint(mlist.get(3));
                } else {
                    if (mlist.size() > 8) {
                        int index = movingdistance / XoffSet;
                        if (index > 0) {
                            int remainderindex = movingdistance % XoffSet;
                            if (remainderindex >= 50) {  //移的那个格字上
                                NLogger.e("TAG", "=======getInformationoint===index====" + index);
                               moveGridMoreRigth(remainderindex,index);
                            } else {  //移动到靠左的格子上
                                moveGridMoreLeft(remainderindex,index);
                            }
                        } else {
                            int remainder = movingdistance % XoffSet;

                            if (remainder >= 50) {  //移动到靠右的那个格字上
                                moveRigth(remainder);
                            } else {  //移动到靠左的格子上
                                moveLeft(remainder);  //移动到靠左
                            }
                        }
                    } else {
                        onScrollstopListner.onScrollToMiddlePoint(mlist.get(4));
                    }
                }
            }
        }
    }

    private void moveGridMoreLeft(int remainderindex, int index) {
        final int  movinglongLeft = movingdistance - remainderindex;
        if(mlist.size() > index+3){
            onScrollstopListner.onScrollToMiddlePoint(mlist.get(index+3));
            new Thread(new Runnable() {
                @Override
                public void run() {
                    scrollTo(movinglongLeft, 0);
                    postInvalidateDelayed(10);
                }
            }).start();
        }

    }

    private void moveGridMoreRigth(int remainderindex, int index) {
        final int  movinglongGridMoreRigth = movingdistance + (XoffSet - remainderindex);
        if(mlist.size() > index+4){
            onScrollstopListner.onScrollToMiddlePoint(mlist.get(index+4));
            //NLogger.e("index", "=======getInformationoint===movelong====" + movinglong);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    scrollTo(movinglongGridMoreRigth, 0);
                    postInvalidateDelayed(10);
                }
            }).start();
        }

    }

    private void moveLeft(int remainder) {
        final int  movinglongLeft = movingdistance - remainder;
        onScrollstopListner.onScrollToMiddlePoint(mlist.get(3));
        new Thread(new Runnable() {
            @Override
            public void run() {
                scrollTo(movinglongLeft, 0);
                postInvalidateDelayed(10);
            }
        }).start();
    }

    private void moveRigth(int remainder) {
      final int  movinglongRigth = movingdistance + (XoffSet - remainder);
        onScrollstopListner.onScrollToMiddlePoint(mlist.get(4));
        //NLogger.e("index", "=======getInformationoint===movelong====" + movinglong);
        new Thread(new Runnable() {
            @Override
            public void run() {
                scrollTo(movinglongRigth, 0);
                postInvalidateDelayed(10);
            }
        }).start();

    }


}
